package com.shard2.service;

import com.shard2.mapper.TestShardMapper;
import io.shardingsphere.core.constant.transaction.TransactionType;
import io.shardingsphere.core.transaction.TransactionTypeHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/4/1  18:23
 * 描述:
 */
@Component
public class TestShardService {

    @Autowired
    private TestShardMapper testShardMapper;

    @Transactional(rollbackFor = Exception.class)
    public void testInsert(){

        TransactionTypeHolder.set(TransactionType.XA);

        Map<String,Object> map=null;
        int tt=0;
        for(int i=1;i<=5;i++){
            tt++;
            map=new HashMap<>();
            Long orderId=System.currentTimeMillis()+i;
            map.put("id",orderId);
            map.put("orderName","UUU"+new Random().nextInt(6000));
            map.put("userId",i);

            testShardMapper.addOne(map);
            Map<String,Object> map2=null;
            for(int g=0;g<=4;g++){
                map2=new HashMap<>();
                map2.put("id",System.currentTimeMillis()+g);
                map2.put("orderId",orderId);
                map2.put("userId",i);
                map2.put("itemName","YYY"+new Random().nextInt(10000));
                testShardMapper.addItems(map2);
            }


        }

        if(tt==5){
           throw new RuntimeException("=======================");
        }


    }

    public List<Map<String,Object>> getOrderList(String orderName){
        return testShardMapper.getOrderList(orderName);
    }


    public List<Map<String,Object>> getOrderItemList(String orderName){
        return testShardMapper.getOrderItemList();
    }

}
